﻿#region

using System;

#endregion

namespace JsMapper.Helper
{
    public static class JsTypeConverter
    {
        public static string ToJsFormatted(this object obj)
        {
            var v = obj.ToString();

            float floatVal;
            var isNmbr = float.TryParse(v, out floatVal);
            if (isNmbr) return v;

            bool boolVal;
            var isBool = bool.TryParse(v, out boolVal);
            if (isBool) return v.ToLower();

            return string.Format("'{0}'", v);
        }

        public static string GetJsType(this Type obj)
        {
            if (obj == typeof(string)) return "string";
            if (obj == typeof(int) || obj == typeof(float) || obj == typeof(decimal) || obj == typeof(Int64) || obj == typeof(Int16)) return "number";
            if (obj == typeof(bool)) return "boolean";

            return "object";
        }
    }
}